package com.baidu.controller;

import com.baidu.pojo.Assess;
import com.baidu.pojo.Order;
import com.baidu.pojo.ReturnJson;
import com.baidu.pojo.User;
import com.baidu.service.AssessService;
import com.baidu.service.ExhibitionService;
import com.baidu.service.OrdersService;
import com.baidu.utils.Pages;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/assess")
public class AssessController {
    @Autowired
    private AssessService assessService;
    @Autowired
    private OrdersService orderService;
    //  查询评论
    @RequestMapping("/getAssessByUid")
    public String getAssessByUid(HttpSession session){
        User u =(User) session.getAttribute("user1");
        List<Assess> as = assessService.getAssessByUid(u.getUid());
        session.setAttribute("assess1",as);
        return "AssessCenter";
    }
    //    修改评论
    @RequestMapping("/updateAssessByAid")
    @ResponseBody
    public ReturnJson updateAssessByAid(Assess assess,HttpSession session){
        Date date=new Date();
        assess.setAtime(date);
        assessService.updateAssessByAid(assess);
        ReturnJson returnJson = new ReturnJson();
        User u =(User) session.getAttribute("user1");
        List<Assess> as = assessService.getAssessByUid(u.getUid());
        session.setAttribute("assess1",as);
        returnJson.setCode(200);
        returnJson.setMsg("OK");
        return returnJson;
    }
    //    删除评论
    @RequestMapping("/delAssessByAid")
    public String delAssessByAid(Integer aid){
        assessService.delAssessByAid(aid);
        return "redirect:/assess/getAllAssessByUIdByPage";
    }
    //    添加评论
    @RequestMapping("/addAssess")
    @ResponseBody
    public ReturnJson addAssess(Assess assess, HttpSession session, Integer oid){
        User u =(User) session.getAttribute("user1");
        Date date = new Date();assess.setAtime(date);assess.setUid(u.getUid());
        Order o = new Order();
        o.setOid(oid);
        o.setOstate(4);
        orderService.updateOrderState(o);
        assessService.addAssess(assess);
        ReturnJson returnJson = new ReturnJson();
        List<Order> or = orderService.getAllOrderByUId(u.getUid());
        session.setAttribute("order1",or);
        returnJson.setCode(200);
        returnJson.setMsg("OK");
        return returnJson;
    }
//    跳转
    @RequestMapping("/goList")
    public String goList(){
        return "order";
    }
    @RequestMapping("/getAllAssessByUIdByPage")
    public String getAllAssessByUIdByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                               @RequestParam(defaultValue = "8") Integer pageSize,
                               HttpServletRequest request,
                               HttpSession session){
        System.out.println("------------getAllAssessByUIdByPage--------------");
        System.out.println("current:"+currentPage);
        User user =(User) session.getAttribute("user1");
        //创建pages对象
        Pages pages=new Pages();
        //设置当前页
        pages.setCurrentPage(currentPage);
        //设置页面容量
        pages.setPageSize(pageSize);
        //查询总记录数
        int total=assessService.getAllAssessByUIdCount(user.getUid());
        //设置总记录数
        pages.setTotalCount(total);
        //将pages对象保存到request中
        request.setAttribute("pages",pages);
        //调用service，获取查询结果
        List<Assess> list = assessService.getAllAssessByUIdByPage(pages.getCp(),pageSize,user.getUid());
        //将集合保存到request中
        session.setAttribute("assess1",list);
        return "AssessCenter";
    }
}
